from forum.models import Forum, Post, Topic
from django.contrib.auth.models import User
from datetime import datetime,timedelta
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.core.mail import send_mail
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required,user_passes_test
from utils import log
from portal.forms import LoginForm, RegistrationForm, ProfileForm
import operator
import random, sha
from calendar import month_name
from forum import forms

@login_required
def new_topic(request,forum_id=None):
    """
    Creates a new topic for whatever forum
    
    """
    if request.POST:
        form = forms.TopicForm(request.POST)
        if form.is_valid():
            topic = form.save(request.user)
            return HttpResponseRedirect('/forum/topics/%s' % topic.id)       
    else:
        form = forms.TopicForm()
        if forum_id:
            form['forum_id']=forum_id
    context = dict(form = form.as_ul())
    return render_to_response('forum/new_topic.html', context, RequestContext(request))  
    

def forums(request):
    """
    This will display a list of forums
    
    """
    forums = Forum.active.order_by('orderby')
    context =dict(forums=forums)
    return render_to_response('forum/main.html', context, RequestContext(request))  

def topics(request,forum_id):
    """
    This will display topics for selected forum
    
    """
    forum = get_object_or_404(Forum,id=forum_id)
    topics = Topic.active.filter(forum=forum).order_by('-date')
    context=dict(topics=topics)
    return render_to_response('forum/topics.html', context, RequestContext(request))  


def topic(request,topic_id):
    """
    Displays topic
    
    """
    topic = get_object_or_404(Topic,id=topic_id)
    context = dict(topic = topic)
    if request.POST and request.user.is_authenticated():
       #only authenticated users can post
       preview = request.POST.get('preview')
       submit = request.POST.get('submit')
       
       content = request.POST.get('content')
       if content:
           if preview:    
               context['preview'] = True
               context['content'] = content
           elif submit:
              post = Post()
              post.content = content
              post.topic = topic
              post.author = request.user
              post.save()
    context['posts'] = posts = Post.active.filter(topic=topic_id).order_by('date')
    return render_to_response('forum/topic.html', context, RequestContext(request))  

def fresh(request):
    """
    This will display the latest activity on forums
    
    """
    topics = Topic.active.order_by('-date')[:10]
    return render_to_response('forum/fresh.html', dict(topics=topics), RequestContext(request))  




################################################################################
#
#     NOT YET IMPLEMENTED 
#
################################################################################

    
@login_required    
@user_passes_test(lambda u: u.is_staff())
def control_topic(request,topic_id):
    """This will change state of topic"""
    
@login_required    
@user_passes_test(lambda u: u.is_staff())
def control_post(request,post_id):
    """This will change the state of post"""
    pass

@login_required
@user_passes_test(lambda u: u.is_staff())
def change_post(request):
    pass

@login_required
@user_passes_test(lambda u: u.is_staff())
def change_topic(request):
    pass

@login_required
def save_topic(request):
    form = forms.TopicForm(request.POST)
    if form.is_valid():
        topic = form.save()
        return HttpResponseRedirect('/forum/topics/%s' % topic.id)
    context = dict(forum_id=form.forum_id,form=form)
    return render_to_response('forum/new_topic.html', context, RequestContext(request))  
    
    
@login_required
def save_post(request):
    form = forms.PostForm(request.POST)
    if form.is_valid():
        topic = form.save()
        return HttpResponseRedirect('/forum/topics/%s#last' % topic.id)
    context = dict(form=form)
    return render_to_response('forum/topics/%s#last'%topic.id, context, RequestContext(request))  
